package com.ggh.jinjilive.ui.home.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemLiveListBinding;
import com.ggh.jinjilive.databinding.ItemLiveNavBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.bumptech.glide.Glide;

import java.util.List;

public class LiveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<LiveList> liveListList;

    public static final int TYPE_ITEM = 0;
    public static final int TYPE_FOOTER = 1;

    private String selected = "0";

    public LiveAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setAdd(List<LiveList> liveListList) {
        this.liveListList = liveListList;
        notifyDataSetChanged();
    }

    public void Add(List<LiveList> liveListList) {
        if(null != this.liveListList){
            this.liveListList.addAll(liveListList);
        }else {
            this.liveListList = liveListList;
        }
        notifyDataSetChanged();
    }

    public void setSelected(String selected) {
        this.selected = selected;
        notifyDataSetChanged();
    }



    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            ItemLiveNavBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_live_nav, parent, false);
            return new LiveNavHolder(binding);
        }else {
            ItemLiveListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_live_list, parent, false);
            return new ItemViewHolder(binding);
        }

    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        if (recyclerViewholder instanceof LiveNavHolder) {//头部
            LiveNavHolder holder = (LiveNavHolder) recyclerViewholder;
            holder.binding.normalRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background);
            holder.binding.normalRoomTab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected("0");
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(0,"0");
                    }
                }
            });
            holder.binding.chargeRequiredRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background);
            holder.binding.passwordRequiredRoomTab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected("1");
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(0,"1");
                    }
                }
            });
            holder.binding.passwordRequiredRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background);
            holder.binding.chargeRequiredRoomTab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected("2");
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(0,"2");
                    }
                }
            });
            holder.binding.timeLimitRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background);
            holder.binding.timeLimitRoomTab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected("3");
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(0,"3");
                    }
                }
            });
            switch (selected){
                case "0":
                    holder.binding.normalRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background_1);
                    break;
                case "1":
                    holder.binding.passwordRequiredRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background_1);
                    break;
                case "2":
                    holder.binding.chargeRequiredRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background_1);
                    break;
                case "3":
                    holder.binding.timeLimitRoomTab.setBackgroundResource(R.mipmap.ic_room_type_background_1);
                    break;
            }
        }else if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            LiveList liveList = liveListList.get(position);
            if (null != liveList) {
                Glide.with(holder.binding.RoundedImageView.getContext()).load(liveList.getCover()).into(holder.binding.RoundedImageView);
                holder.binding.tvNumber.setText(liveList.getNumber() +"");
                if (liveList.getType() == 0){
                    holder.binding.roomType.setText("普通");
                }else if (liveList.getType() == 1){
                    holder.binding.roomType.setText("密码");
                }else if (liveList.getType() == 2){
                    holder.binding.roomType.setText("付费");
                }else if (liveList.getType() == 3) {
                    holder.binding.roomType.setText("计时");
                }
                holder.binding.tvName.setText(liveList.getTitle());
                holder.binding.tvName.setSelected(true);
                holder.binding.llLive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(position,liveList);
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        if(null != liveListList && liveListList.size() > 0){
            return liveListList.size();
        }else {
            return 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ITEM;
        }else {
            return TYPE_FOOTER;
        }
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemLiveListBinding binding;

        public ItemViewHolder(@NonNull ItemLiveListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.llLive.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 2 - SizeUtils.dp2px(15);
            binding.llLive.setLayoutParams(params);
        }
    }


    class LiveNavHolder extends RecyclerView.ViewHolder {
        ItemLiveNavBinding binding;

        public LiveNavHolder(@NonNull ItemLiveNavBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}